<?php

class Post_model extends CI_Model {

    private $html5_blog__posts = 'posts';

    function __construct() {
        parent::__construct();
    }

    function list_all() {
        $this->db->order_by('id', 'asc');
        return $this->db->get($this->html5_blog__posts);
    }

    function count_all($post_type) {
        $this->db->like('post_type', $post_type);
        $this->db->from($this->html5_blog__posts);
        return $this->db->count_all_results();
    }

    /**
     * This function is used to get all posts based on the page.
     * 
     * @param type $limit
     * @param type $offset
     * @param type $post_type
     * @return array of post
     */
    function get_paged_list($limit = 10, $offset = 0, $post_type = 'post') {
        $this->db->where('post_type', $post_type);
        $this->db->order_by('id');
        $obj_query = $this->db->get($this->html5_blog__posts, $limit, $offset);
        return $obj_query;
    }

    function get_by_id($id) {
        $this->db->where('id', $id);
        return $this->db->get($this->html5_blog__posts);
    }

    function save($person) {
        $this->db->insert($this->html5_blog__posts, $person);
        return $this->db->insert_id();
    }

    function update($id, $person) {
        $this->db->where('id', $id);
        $this->db->update($this->html5_blog__posts, $person);
    }

    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete($this->html5_blog__posts);
    }
    
    /**
    * Retrieves post data given a post ID or post object.
    *
    * See {@link sanitize_post()} for optional $filter values. Also, the parameter
    * $post, must be given as a variable, since it is passed by reference.
    *
    * @since 1.5.1
    * @uses $wpdb
    * @link http://codex.wordpress.org/Function_Reference/get_post
    *
    * @param int|object $post Post ID or post object.
    * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
    * @return mixed Post data
    */
    function &get_post(&$post, $output = OBJECT) {
            global $wpdb;
            $null = null;

            if ( empty($post) ) {
                    if ( isset($GLOBALS['post']) )
                            $_post = & $GLOBALS['post'];
                    else
                            return $null;
            } elseif ( is_object($post) && empty($post->filter) ) {
                    _get_post_ancestors($post);
                    $_post = sanitize_post($post, 'raw');
                    wp_cache_add($post->ID, $_post, 'posts');
            } elseif ( is_object($post) && 'raw' == $post->filter ) {
                    $_post = $post;
            } else {
                    if ( is_object($post) )
                            $post_id = $post->ID;
                    else
                            $post_id = $post;

                    $post_id = (int) $post_id;
                    if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
                            $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
                            if ( ! $_post )
                                    return $null;
                            _get_post_ancestors($_post);
                            $_post = sanitize_post($_post, 'raw');
                            wp_cache_add($_post->ID, $_post, 'posts');
                    }
            }
            
            if ( $output == OBJECT ) {
                    return $_post;
            } elseif ( $output == ARRAY_A ) {
                    $__post = get_object_vars($_post);
                    return $__post;
            } elseif ( $output == ARRAY_N ) {
                    $__post = array_values(get_object_vars($_post));
                    return $__post;
            } else {
                    return $_post;
            }
    }

}

?>